package model.forview;

import java.util.Iterator;
import java.util.Map;

import model.wrap.node.Node;
import models.ConstantName;
import models.ConstantValue;

public class NodeChangeModelForView extends ContentForMainTemplate
{
    public Long nodeId;
    public String title;

    public final String nodeIdName = "nodeId";
    public final String titleName = "title";

    public String saveButton;
    public String cancelButton;

    public String typeLabel;
    public String structureLabel;
    public String titleLabel;

    public NodeChangeModelForView(Node node)
    {
	setDefaultValues();
	this.nodeId = node.getId();
	this.title = node.getTitle();
    }

    public NodeChangeModelForView(Map<String, String> form)
    {
	setDefaultValues();
	
	Iterator<String> keys = form.keySet().iterator();
	while(keys.hasNext())
	{
	    String key = keys.next();
	    if(key.equals("data["+this.nodeIdName+"]"))
		this.nodeId = Long.valueOf(form.get(key));
	    if(key.equals("data["+this.titleName+"]"))
		this.title = form.get(key);
	}
    }

    protected void setDefaultValues()
    {
	String title = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_PAGE_TITLE);
	String header = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_PAGE_HEADER);
	String footer = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_PAGE_FOOTER);
	String userName = "";

	title = title != null ? title : "Node create";
	header = header != null ? header : "Node create";
	footer = footer != null ? footer : "Node create";

	titleLabel = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_TITLE_LABEL);
	typeLabel = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_TYPE_LABEL);
	structureLabel = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_STRUCTURE_LABEL);
	saveButton = ConstantValue.getConstantValue(ConstantName.SAVE_BUTTON);
	cancelButton = ConstantValue.getConstantValue(ConstantName.CANCEL_BUTTON);

	titleLabel = titleLabel != null ? titleLabel : "Title";
	typeLabel = typeLabel != null ? typeLabel : "Type";
	structureLabel = structureLabel != null ? structureLabel : "Structure";
	saveButton = saveButton != null ? saveButton : "Save";
	cancelButton = cancelButton != null ? cancelButton : "Cancel";

	mainModelForView = new MainModelForView(title, userName, header, footer);
    }

}
